﻿// -----------------------------------------------------------------------
// <copyright file="DatabaseProducer.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Dhgms.TplHelper.Model.DataReaderProducer
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public abstract class DatabaseProducer<TDatabaseHelper, TParameter, TParameterCollection> : DataReaderProducerBase
        where TDatabaseHelper : Dhgms.DataManager.Model.Helper.Database.DatabaseHelperBase<TParameter, TParameterCollection>, new()
        where TParameter : DbParameter
        where TParameterCollection : DbParameterCollection
    {
        private readonly string connectionString;

        /// <summary>Initializes a new instance of the <see cref="DatabaseProducer{TDatabaseHelper,TSqlParameter}"/> class.</summary>
        /// <param name="connectionString">The connection string.</param>
        protected DatabaseProducer(string connectionString)
        {
            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new ArgumentNullException("connectionString");
            }

            this.connectionString = connectionString;
        }

        /// <summary>Gets the SQL parameters.</summary>
        public abstract TParameter[] SqlParameters { get; }

        /// <summary>Gets the SQL command.</summary>
        public abstract string SqlCommand { get; }

        /// <summary>Gets a value indicating whether to assume the SQL is safe.</summary>
        public virtual bool AssumeSqlSafe
        {
            get
            {
                return false;
            }
        }

        /// <summary>Produces and Consumes the data</summary>
        /// <param name="dataReaderConsumingAction">The data reader consuming action.</param>
        /// <returns>Number of records processed.</returns>
        public override long ProduceAndConsume(Func<IDataReader, long> dataReaderConsumingAction)
        {
            var dbHelper = new TDatabaseHelper();
            return dbHelper.ActUponDataReader(this.connectionString, this.SqlCommand, this.SqlParameters, dataReaderConsumingAction, this.AssumeSqlSafe);
        }
    }
}
